package collection;

import org.junit.Test;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by :Infaraway
 * DATE : 2017/3/26
 * Time : 16:10
 * Funtion :
 */
public class MapTest {

    @Test
    public void testLinkedHashMap(){
        Map map = new LinkedHashMap<>();

        map.put("CC", new Person("CC", 18));
        map.put("AA", new Person("AA", 10));
        map.put("DD", new Person("DD", 12));
        map.put("BB", new Person("BB", 16));

        /*Iterator it = map.keySet().iterator();
        while (it.hasNext()){
            Object key = it.next();
            Object value = map.get(key);
            System.out.println(key+":"+value);
        }
        System.out.println();*/

        for (Object key : map.keySet()){
            Object value = map.get(key);
            System.out.println(key+":"+value);
        }
    }

    @Test
    public void testTreeMap(){
        Map map = new TreeMap<>();

        //这里使用加入Compareable接口的Person类，这样才可以进行排序
        map.put(new PersonCompare("CC", 18),"CC");
        map.put(new PersonCompare("AA", 10),"AA");
        map.put(new PersonCompare("DD", 12),"DD");
        map.put(new PersonCompare("BB", 16),"BB");

        for (Object key : map.keySet()){
            Object value = map.get(key);
            System.out.println(key+":"+value);
        }
    }
}
